<?php
/**
 *
 * User: wufeng
 * Date: 2020/01/08
 */
namespace app\xcxapi\service;

use app\common\service\BaseService;
use app\lib\exception\BaseException;
use app\models\CampusModel;
use app\models\CourseModel;
use app\models\DictionaryModel;
use app\models\GradesDetailsModel;
use app\models\ScheduleModel;
use app\models\StudentSubjectClassModel;

class CourseService extends BaseService
{
	/**
	 * 课程
	 * User: wufeng
	 * Date: 2020/01/08
	 *
	 * @param $params
	 * @return array
	 */
	public function courseLists($params)
	{
		try {
			$where = [];
			if (isset($params['course_name']) && '' !== $params['course_name']) {
				$where['course_name'] = ['like', '%'.$params['course_name'].'%'];
			}
			$where['campus_id'] = ['in',xcxapi_get_current_campus_id()];
			$order = 'id desc';
				$this->data = (new CourseModel())
				->where($where)
				->order($order)
				->select()
				->toArray();

			foreach ($this->data as &$item) {
				$item['price'] = $item['price'] / 100;
				$item['course_type_text'] = DictionaryModel::getById($item['course_type'],'name');
				switch ($item['status']){
					case 0:
						$item['status_text'] = '禁用';
						break;
					case 1:
						$item['status_text'] = '启用';
						break;
				};
			}
			if (empty($this->data)) {
				throw new BaseException('NO_DATA');
			}
		} catch (\Exception $e) {
			$this->errorMessage($e, $params);
		}
		return $this->return;
	}

	/**
	 * 新增课程
	 * User: wufeng
	 * Date: 2020/01/11
	 *
	 * @param $params
	 * @return array
	 */
	public function courseAdd($params)
	{
		try {
			//			$data['campus_id'] = xcxapi_get_current_campus_id();
			$data = [];
			$data['campus_id'] = $params['campus_id'];
			$data['price'] = $params['price'] * 100;
			$data['cost_type'] = $params['cost_type'];
			$data['subject'] = $params['subject'];
			$data['class_num'] = $params['class_num'];
			$data['course_name'] = $params['course_name'];
			$data['student_grade'] = $params['student_grade'];
			$data['course_type'] = $params['course_type'];
			$data['class_type'] = $params['class_type'];
			$data['course_quarter'] = $params['course_quarter'];
			$data['course_year'] = $params['course_year'];
			$data['class_time'] = $params['class_time'];
			$data['describe'] = $params['describe'];
			if($data['cost_type'] == 97){
				$data['unit'] = '期';
			}elseif ($data['cost_type'] == 95){
				$data['unit'] = '小时';
				$data['class_num'] = 1;
				$data['class_time'] = 60;
			}elseif ($data['cost_type'] == 89){
				$data['unit'] = '课次';
				$data['class_num'] = 1;
			}
			if($data['subject'] == 131){
				$data['is_allsubject'] = 1;
			}else{
				$data['is_allsubject'] = 2;
			}
			$data['total_price'] = $data['price'] *  $data['class_num'];
			(new CourseModel())->save($data);
		} catch (\Exception $e) {
			$this->errorMessage($e, $params);
		}

		return $this->return;
	}

	/**
	 * 编辑课程
	 * User: wufeng
	 * Date: 2020/01/11
	 *
	 * @param $params
	 * @return array
	 */
	public function coursEdit($params)
	{
		try {
			//			$data['campus_id'] = xcxapi_get_current_campus_id();
			$data = [];
			$data['campus_id'] = 1;
			$data['price'] = $params['price'] * 100;
			$data['cost_type'] = $params['cost_type'];
			$data['subject'] = $params['subject'];
			$data['class_num'] = $params['class_num'];
			$data['course_name'] = $params['course_name'];
			$data['student_grade'] = $params['student_grade'];
			$data['course_type'] = $params['course_type'];
			$data['class_type'] = $params['class_type'];
			$data['course_quarter'] = $params['course_quarter'];
			$data['course_year'] = $params['course_year'];
			$data['class_time'] = $params['class_time'];
			$data['describe'] = $params['describe'];
			$data['total_price'] = $data['price'] *  $data['class_num'];
			if($data['cost_type'] == 97){
				$data['unit'] = '期';
			}elseif ($data['cost_type'] == 95){
				$data['unit'] = '小时';
				$data['class_time'] = 60;
			}elseif ($data['cost_type'] == 89){
				$data['unit'] = '课次';
			}
			(new CourseModel())->save($data, ['id' => $params['id']]);
		} catch (\Exception $e) {
			$this->errorMessage($e, $params);
		}

		return $this->return;
	}

	/**
	 * 课程 启用/禁用
	 * User: wufeng
	 * Date: 2020/01/08
	 *
	 * @param $params
	 * @return array
	 */
	public function courseStatus($params)
	{
		try {
			$courseModel = (new CourseModel())->where(['id'=>$params['id']])->find();
			if($courseModel['status'] === 1){
				$data['status'] = 0;
			}else{
				$data['status'] = 1;
			}
			(new CourseModel())->save($data,['id'=>$params['id']]);
		} catch (\Exception $e) {
			$this->errorMessage($e, $params);
		}

		return $this->return;
	}

	/**
	 * 删除课程
	 * User: wufeng
	 * Date: 2020/01/11
	 *
	 * @param $params
	 * @return array
	 */
	public function courseDel($params)
	{
		try {
			(new CourseModel())->where(['id'=>$params['id']])->delete();
		} catch (\Exception $e) {
			$this->errorMessage($e, $params);
		}

		return $this->return;
	}

	/**
	 * 课程详情/班级列表/课程目录
	 * User: wufeng
	 * Date: 2020/02/10
	 *
	 * @param $params
	 * @return array
	 */
	public function courseDetail($params)
	{
		try {
			$where = $schedule =  [];
			$where['course_id'] = $schedule['course_id'] = $params['id'];
//			$where['campus_id'] = xcxapi_get_current_campus_id();
//			$where['campus_id'] = $schedule['campus_id'] = 1;
			$where['graduation_status'] = 0;
			$course_infos = (new CourseModel())->get(['id' => $params['id']]);//课程
			if($course_infos['student_grade'] != 0){
				$course_infos['student_grade'] = (new DictionaryModel())->get(['id' => $course_infos['student_grade']])['name'];
			}else{
				$course_infos['student_grade'] = '--';
			}
			$course_infos['course_type'] = (new DictionaryModel())->get(['id' => $course_infos['course_type']])['name'];
			if($course_infos['status'] == 1){
				$course_infos['status'] = '有效';
			}else{
				$course_infos['status'] = '无效';
			}

			$course_infos['price'] =  ($course_infos['price'] / 100);
			if($course_infos['describe'] == null){
				$course_infos['describe'] = '--';
			}

			$this->data['course_info'] = $course_infos;
			$this->data['class'] = (new GradesDetailsModel())
				->where($where)
				->select()
				->toArray();
			foreach ($this->data['class'] as &$class) {
				$arrs = (new StudentSubjectClassModel())->where('class_subject_id',$class['id'])->column('student_id');
				$class['num'] = count(array_unique($arrs));
				$class['room'] = (implode('|',array_unique(explode(',',$class['room_text']))));
				$class['course_name'] = (new CourseModel())->get(['id' => $class['course_id']])['course_name'];
			}

			$this->data['schedule'] = (new ScheduleModel())
				->where($schedule)
				->select()
				->toArray();
		} catch (\Exception $e) {
			$this->errorMessage($e, $params);
		}
		return $this->return;
	}

	/**
	 * 年级/科目/期段/类型/班型/计费规则/
	 * User: wufeng
	 * Date: 2020/02/11
	 *
	 */
	public function achieveLists()
	{
		try {

			//年级
			$this->data['class'] = (new DictionaryModel())
				->where('type',1)
				->field('id,name')
				->select()
				->toArray()
			;

			//科目
			$this->data['subject'] = (new DictionaryModel())
				->where('type',10)
				->field('id,name')
				->select()
				->toArray()
			;

			//年份
			$this->data['year'] = (new DictionaryModel())
				->where('type',12)
				->field('id,name')
				->select()
				->toArray()
			;

			//期段
			$this->data['stage'] = (new DictionaryModel())
				->where('type',21)
				->field('id,name')
				->select()
				->toArray()
			;

			//类型
			$this->data['type'] = (new DictionaryModel())
				->where('type',22)
				->field('id,name')
				->select()
				->toArray()
			;

			//班型
			$this->data['class_type'] = (new DictionaryModel())
				->where('type',9)
				->field('id,name')
				->select()
				->toArray()
			;

			//计费规则
			$this->data['cost_type'] = (new DictionaryModel())
				->where('type',23)
				->field('id,name')
				->select()
				->toArray()
			;

			if (empty($this->data)) {
				throw new BaseException('NO_DATA');
			}

		} catch (\Exception $e) {
			$this->errorMessage($e, '');
		}

		return $this->return;
	}

	/**
	 * 新增课程目录
	 * User: wufeng
	 * Date: 2020/02/11
	 *
	 * @param $params
	 * @return array
	 */
	public function scheduleAdd($params)
	{
		try {
			$data = [];
			//			$data['campus_id'] = xcxapi_get_current_campus_id();
			$data['campus_id'] = 1;
			$data['course_id'] = $params['course_id'];
			$data['class_times'] = $params['class_times'];
			$data['class_chapter'] = $params['class_chapter'];
			$data['class_content'] = $params['class_content'];
			(new ScheduleModel())->save($data);
		} catch (\Exception $e) {
			$this->errorMessage($e, $params);
		}

		return $this->return;
	}

	/**
	 * 编辑课程目录
	 * User: wufeng
	 * Date: 2020/02/11
	 *
	 * @param $params
	 * @return array
	 */
	public function scheduleEdit($params)
	{
		try {
			$data = [];
			//			$data['campus_id'] = xcxapi_get_current_campus_id();
			$data['campus_id'] = 1;
			$data['class_times'] = $params['class_times'];
			$data['class_chapter'] = $params['class_chapter'];
			$data['class_content'] = $params['class_content'];
			(new ScheduleModel())->save($data, ['id' => $params['id']]);
		} catch (\Exception $e) {
			$this->errorMessage($e, $params);
		}

		return $this->return;
	}
	
	/**
	 * 删除课程目录
	 * User: wufeng
	 * Date: 2020/04/19
	 *
	 * @param $params
	 * @return array
	 */
	public function scheduleDel($params)
	{
		try {
            if (!(new ScheduleModel())->where(['id' => $params['id']])->delete()) {
                throw new BaseException('DELETE_SCHEDULE_FAILED');
            }
		} catch (\Exception $e) {
			$this->errorMessage($e, $params);
		}

		return $this->return;
	}
}
